﻿/*
 * 版 本 V1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2018.03.12
 * 描 述：数据库表复制	
 */
var acceptClick;
var dbKey = '';
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.inittree();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
        },
        inittree: function () {
            $('#db_tree').lrtree({
                url: config.base_server() + 'dbLinks/tree?access_token=' + config.getToken().access_token,
                nodeClick: page.treeNodeClick
            });
        },
        treeNodeClick: function (item) {
            if (!item.hasChildren) {
                dbKey = item.id;
                $('#titleinfo').html('[' + item.text + ']&nbsp;[' + item.value + ']');
                page.search();
            }
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'dbTables?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "表名", name: "tableName", width: 300, align: "left"},
                    {
                        label: "记录数", name: "tableRows", width: 100, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue + "条";
                        }
                    },
                    {label: "使用大小", name: "dataLength", width: 100, align: "center"},
                    {label: "索引使用大小", name: "indexLength", width: 120, align: "center"},
                    {label: "说明", name: "tableComment", width: 200, align: "left"}
                ],

                reloadSelected: true,
                mainId: 'name',
                onSelectRow: function (rowdata) {
                    selectedRow = rowdata.tableName;
                },
                isSubGrid: true,             // 是否有子表
                subGridExpanded: function (subid, rowdata) {
                    $('#' + subid).jfGrid({
                        url: config.base_server() + 'dbTables/listFields?access_token=' + config.getToken().access_token,
                        headData: [
                            {label: "列名", name: "columnName", width: 300, align: "left"},
                            {label: "数据类型", name: "dataType", width: 100, align: "center"},
                            {label: "长度", name: "columnType", width: 120, align: "center"},
                            {
                                label: "允许空", name: "isNullable", width: 50, align: "center",
                                formatter: function (cellvalue) {
                                    return cellvalue == 'YES' ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                                }
                            },
                            {label: "标识", name: "ordinalPosition", width: 58, align: "center"},
                            {
                                label: "主键", name: "columnKey", width: 50, align: "center",
                                formatter: function (cellvalue) {
                                    return cellvalue == 'PRI' ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                                }
                            },
                            {label: "默认值", name: "columnDefault", width: 120, align: "center"},
                            {label: "说明", name: "columnComment", width: 200, align: "left"}
                        ]
                    });
                    $('#' + subid).jfGridSet('reload', {dbKey: dbKey, tableName: rowdata.tableName});
                }// 子表展开后调用函数
            });
        },
        search: function (param) {
            param = param || {};
            param.dbKey = dbKey;
            $('#gridtable').jfGridSet('reload', param);
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        callBack({dbKey: dbKey, tableName: selectedRow});
        return true;
    };
    page.init();
}


